package com.bnz.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.ChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bnz.Student;
import com.bnz.mapper.ClassesMapper;
import com.bnz.mapper.StudentMapper;
import com.bnz.service.StudentService;
import com.bnz.until.PageResult;
import com.bnz.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service // 注入到spring容器中
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    // 导入ClassesMapper
    @Autowired
    private ClassesMapper classesMapper;

    // 1、查找数据
    @Override
    public List<Student> findAll() {
        List<Student> studentList = getBaseMapper().selectList(null);// 查找全部
        return studentList;
    }

    // 2、添加数据
    @Override
    public void add(Student student) {
        getBaseMapper().insert(student);
    }

    // 3、分页查询
    @Override
    public PageResult<Student> findByPage(Integer page, Integer pagesize) {
        // 3.1、定义出参数页的对象
        IPage<Student> studentPage = new Page<>(page, pagesize);
        // 3.2、条件查询
        Wrapper<Student> wrapper = new QueryWrapper<>();
        // 3.3、进行分页查询
        IPage<Student> studentIPage = baseMapper.selectPage(studentPage, wrapper);
        // 3.4、遍历查询到的数据，并为其指定班级
        for (Student student : studentIPage.getRecords()) {
            String cname = classesMapper.selectById(student.getCid()).getCname();
            student.setCname(cname);
        }
        // 3.5、返回
        return new PageResult<>(studentIPage.getTotal(), studentIPage.getRecords());
    }

    // 4、条件查询带分页
    @Override
    public PageResult<Student> search(Integer page, Integer pagesize, StudentVo studentVo) {
        // 4.1、定义分页查询的对象（页数，一页的数量）
        IPage<Student> studentPage = new Page<>(page, pagesize);
//        // 方法一(传统写法)：
//        // 1、定义条件构造器
//        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
//        // 2、判断前端是否有查询条件（没有就全查）
//        if (null != studentVo){
//            if (!StringUtils.isEmpty(studentVo.getSname())){
//                queryWrapper.like("sname",studentVo.getSname());
//            }
//            if (!StringUtils.isEmpty(studentVo.getAddr())){
//                queryWrapper.like("addr",studentVo.getAddr());
//            }
//            if (null != studentVo.getCid() && studentVo.getCid() != 0){
//                queryWrapper.eq("cid",studentVo.getCid());
//            }
//        }
//        baseMapper.selectPage(studentPage,queryWrapper);

        // 方法二（链式）
        QueryChainWrapper<Student> queryWrapper = new QueryChainWrapper<>(baseMapper);
        queryWrapper.like("sname", studentVo.getSname())
                .like("addr", studentVo.getAddr());
        if (null != studentVo.getCid() && 0 != studentVo.getCid()){
            queryWrapper.eq("cid",studentVo.getCid());
        }

        // 方法三（lamba表达式）
//        LambdaQueryChainWrapper<Student> queryWrapper = new LambdaQueryChainWrapper<>(baseMapper);
//        queryWrapper.like(Student::getSname,studentVo.getSname())
//                .like(Student::getAddr,studentVo.getAddr());
//        if (null != studentVo.getCid() && 0 != studentVo.getCid()){
//            queryWrapper.eq(Student::getCid,studentVo.getCid());
//        }

        // 3、进行分页查询（链式）
        IPage<Student> studentIPage = queryWrapper.page(studentPage);
        // 4、遍历查找到的每一个学生数据，并为其指定班级
        for (Student student : studentIPage.getRecords()){
            String cname = classesMapper.selectById(student.getCid()).getCname();
            student.setCname(cname);

        }
        // 返回数据
        return new PageResult<>(studentIPage.getTotal(),studentIPage.getCurrent());
    }
}
